package com.hl.card.modular.packBack.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import com.hl.card.common.persistence.dao.CommunityUserMapper;
import com.hl.card.common.persistence.dao.UserMapper;
import com.hl.card.common.persistence.model.DistinguishIdCard;
import com.hl.card.common.persistence.model.User;
import com.hl.card.common.util.DateUtils;
import com.hl.card.modular.packBack.model.Config;
import com.hl.card.modular.packBack.service.DistinguishService;

@Service
public class DistinguishServiceImpl implements DistinguishService{
	private static String COLLECTION_NAME = "IdCardInfo";
	@Autowired
	private MongoTemplate mongoTemplate;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private CommunityUserMapper communityUserMapper;
	
	@Override
	public List<DistinguishIdCard> list(Page<DistinguishIdCard> page,String phone,String startTime,String endTime,String status) {
		// TODO Auto-generated method stub
		Query query = new Query();
		query.with(new Sort(new Sort.Order(Sort.Direction.DESC, "add_time")));
		if(!StringUtils.isEmpty(phone))
		{
			query.addCriteria(Criteria.where("phone").is(phone));
		}
		if(!StringUtils.isEmpty(status))
		{
			query.addCriteria(Criteria.where("auth").is(status));
		}else
		{
			query.addCriteria(Criteria.where("auth").ne("0"));
		}
		if(!StringUtils.isEmpty(startTime)&&!StringUtils.isEmpty(endTime))
		{
			startTime=startTime+" 00:00:00";
			endTime=endTime+" 59:59:59";
			query.addCriteria(Criteria.where("add_time").gte(startTime).lte(endTime));
		}
		int skip = (page.getCurrent() - 1) * page.getSize();
		query.skip(skip);// 从那条记录开始
		query.limit(page.getSize());// 取多少条记录
	
		page.setTotal((int) mongoTemplate.count(query, DistinguishIdCard.class,COLLECTION_NAME));
		return mongoTemplate.find(query, DistinguishIdCard.class, COLLECTION_NAME);
	}
	
	
	/**
	 * 审核或者驳回业务逻辑
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public void changeStatus(String user_id, String status, String error_info,String user_name) {
		Query query = Query.query(Criteria.where("user_id").is(user_id));
		if(status.equals("1")||status.equals("3"))  //审核通过或者审核失败
		{
			DistinguishIdCard distinguishIdCard=mongoTemplate.findOne(query, DistinguishIdCard.class, COLLECTION_NAME);
			User user=new User();
			if(status.equals("1"))  //如果审核成功
			{
				user.setIsauth(1);
				user.setName(distinguishIdCard.getName());
				user.setBirthday(DateUtils.strToDate(distinguishIdCard.getBirth(), "yyyyMMdd"));
				Integer sex=0;
				if("男".equals(distinguishIdCard.getSex()))
				{
					sex=1;
				}else
				{
					sex=2;
				}
				user.setSex(sex);
				user.setAuthTime(DateUtils.getNow());  //审核成功时间
				user.setOperationUser(user_name);
				Map<String,Object> param=new HashMap<String, Object>();
				param.put("user_id", user_id);
				communityUserMapper.addBillCount(param);
			}else
			{
			
				user.setIsauth(0);
			
			}
			user.setUserId(user_id);
			user.setDetailinfo(JSON.toJSONString(distinguishIdCard));
			userMapper.updateUserInfo(user);
		}
        Update update=new Update();
		update.set("auth",status);
		update.set("error_info",error_info);
		mongoTemplate.updateFirst(query, update, DistinguishIdCard.class, COLLECTION_NAME); // 保存配置
		
	}


	@Override
	public void cancel(String user_id) {
		Query query = Query.query(Criteria.where("user_id").is(user_id).and("auth").is("1"));
		DistinguishIdCard distinguishIdCard=mongoTemplate.findOne(query, DistinguishIdCard.class, COLLECTION_NAME);
		if(distinguishIdCard!=null)
		{
	        Update update=new Update();
			update.set("auth","2");
			mongoTemplate.updateFirst(query, update, DistinguishIdCard.class, COLLECTION_NAME); // 保存配置
		}
		
	}

}
